<?php
require_once 'BaseController.php';

/**
 * マイページ新規登確認画面コントローラクラス<br />
 *
 * @package apamanshop
 * @author
 * @copyright　
 */
class Mypage_MypagecnfController extends Mypage_BaseController
{
	/**
	 * インデックスアクション
	 *
     * @access public
     * @return void
	 */
	public function indexAction()
	{
		//表示テンプレートパス設定
        $this->_display_path = APPLICATION_PATH . '/modules/mypage/views/scripts/common2.tpl';

        //=====表示用共通パラメータ設定=====
		$this->setCommonParams();

		//=====ブロック設定=====
		$this->setBlock($this->_mypagecnf_helper,$this->_js_mypagecnf_helper);

		//=====表示=====
        $this->display($this->_display_path);
	}



	/**
	 * ＤＢ登録アクション
	 *
     * @access public
     * @return void
	 */
	public function dataputAction()
	{
        //表示テンプレートパス設定
        $this->_display_path = APPLICATION_PATH . '/modules/mypage/views/scripts/common2.tpl';

		//0:成功  1:id必須エラー  2：id不正エラー
		$result = -1;

		$password = null;
		$NickName = null;


        //=====リクエストパラメータ取得=====
        //ID(e-mail)
        $id = (array_key_exists('eID', $this->_params)) ? $this->_params['eID'] : null;
        //パスワード
        $Pass = (array_key_exists('ePass', $this->_params)) ? $this->_params['ePass'] : null;
        //パスワード確認用
        $Pass2 = (array_key_exists('ePass2', $this->_params)) ? $this->_params['ePass2'] : null;
        //ニックネーム
        $NickName = (array_key_exists('eNickName', $this->_params)) ? $this->_params['eNickName'] : null;
        //性別
        $Sex = (array_key_exists('eSex', $this->_params)) ? $this->_params['eSex'] : null;
        //年齢
        $Age = (array_key_exists('eAge', $this->_params)) ? $this->_params['eAge'] : null;
        //メール送付
        $Info = (array_key_exists('eInfo', $this->_params)) ? $this->_params['eInfo'] : null;
        //HTMLメール
        $HTMLMail = (array_key_exists('eHTMLMail', $this->_params)) ? $this->_params['eHTMLMail'] : null;

		//ＤＢに登録してメールを送る
		require_once 'Zend/Http/Client.php';

		$cli = new Zend_Http_Client($this->_sslurl.'mypage/Account/insaccount',
		array(
			'maxredirects' => 1,
			'timeout' => 60
		)
		);

		$cli->setParameterPOST(
			array(
				'mode'=>'json' ,
				'isMobile' => '1' ,
				'eID' => $id,
				'ePass' => $Pass,
				'eNickName' => $NickName,
				'eSex' => $Sex,
				'eAge' => $Age,
				'eInfo' => $Info,
				'eHTMLMail' => $HTMLMail
			)
		);


		$response = $cli->request(Zend_Http_Client::POST);
		$resArray = json_decode($response->getBody(), true);

		$result = $resArray["api_result"]["result"];
//		$userCd = $resArray["data"]["userCd"];
		$userCd = $resArray["data"];

		//==========画面描画関連処理==========
		if ($result == 0002)
		{
			// 後続処理
			$favorites = $this->getFavoriteCookie();
			if (count($favorites) > 0 ) {
				$this->_model = new Mypage_Model_Okiniiribukken();
				$favoritesParam['TorokuKbn'] = 1;
				$favoritesParam['userCd'] = $userCd;

				for ($i = 0; $i < count($favorites); $i++) {
					$favoritesParam['heyaCd'] = $favorites[$i];
					//登録処理
					$result = $this->_model->insertFavoriteBukken($favoritesParam);
				}
			}

			// 完了画面へリダイレクト
			$this->_params['MyUcd'] = $userCd;
			$this->getSession()->_params = $this->_params;
			Apm_Util::redirect('/mypage/mypagecnf/regok');
			exit();

			//ユーザーコードを渡したい！！！
			$this->_data['MyUcd'] = $userCd;
	        //=====表示用共通パラメータ設定=====
			$this->setCommonParams();

			$this->_forward( 'index' , 'mypagefin');
			return;
		} else {

			// 確認画面へリダイレクト
			$this->getSession()->_params = $this->_params;
			Apm_Util::redirect('/mypage/mypagecnf/regng', array('result' => $result));
			exit();

			// メッセージセット
			$this->_data['message'] = Apm_Otheritems::getResMessage($result,"",$this->_params['eID']);
			$this->_data['result'] = $result;
		}

        //=====表示用共通パラメータ設定=====
		$this->setCommonParams();

		//=====ブロック設定=====
		$this->setBlock($this->_mypagecnf_helper,$this->_js_mypagecnf_helper);

		//=====表示=====
       $this->display($this->_display_path);
	}

	/**
	 * 新規登録OKアクション
	 *
     * @access public
     * @return void
	 */
	public function regokAction()
	{
		$this->_params = $this->getSession()->_params;
		$this->_data['loginurl'] = $this->_loginurl;

		//表示テンプレートパス設定
		$this->_display_path = APPLICATION_PATH . '/modules/mypage/views/scripts/common2.tpl';

		//=====表示用共通パラメータ設定=====
		$this->setCommonParams_fin();

		//=====ブロック設定=====
		$this->setBlock($this->_mypagefin_helper,$this->_js_mypagefin_helper);

		//=====表示=====
		$this->display($this->_display_path);
	}

	/**
	 * 新規登録NGアクション
	 *
     * @access public
     * @return void
	 */
	public function regngAction()
	{
		$result = $this->_params['result'];
		$this->_params = $this->getSession()->_params;
		
		// メッセージセット
		$this->_data['message'] = Apm_Otheritems::getResMessage($result,"",$this->_params['eID']);
		$this->_data['result'] = $result;

		//表示テンプレートパス設定
		$this->_display_path = APPLICATION_PATH . '/modules/mypage/views/scripts/common2.tpl';

		//=====表示用共通パラメータ設定=====
		$this->setCommonParams();

		//=====ブロック設定=====
		$this->setBlock($this->_mypagecnf_helper,$this->_js_mypagecnf_helper);

		//=====表示=====
		$this->display($this->_display_path);
	}

	/**
	 * マイページ新規登録確認画面の共通パラメータ設定
	 *
	 */
	private function setCommonParams()
	{
        //ID(e-mail)
        $id = (array_key_exists('eID', $this->_params)) ? $this->_params['eID'] : null;
        $Pass = (array_key_exists('ePass', $this->_params)) ? $this->_params['ePass'] : null;
        $Pass2 = (array_key_exists('ePass2', $this->_params)) ? $this->_params['ePass2'] : null;
        $NickName = (array_key_exists('eNickName', $this->_params)) ? $this->_params['eNickName'] : null;
        $Sex = (array_key_exists('eSex', $this->_params)) ? $this->_params['eSex'] : null;
        $Age = (array_key_exists('eAge', $this->_params)) ? $this->_params['eAge'] : null;
        $Info = (array_key_exists('eInfo', $this->_params)) ? $this->_params['eInfo'] : null;
        $HTMLMail = (array_key_exists('eHTMLMail', $this->_params)) ? $this->_params['eHTMLMail'] : null;
        $MyUcd = (array_key_exists('MyUcd', $this->_params)) ? $this->_params['MyUcd'] : null;


        //TODO:formParam、CHeyaCD、SrhKbnStockはいらないと思う。。。
        //検索条件からのパラメータ引きずり
		$formParam = (array_key_exists('formParam', $this->_params)) ? $this->_params['formParam'] : null;
        //Cookie登録上限部屋CD
		$heyaCd = (array_key_exists('CHeyaCD', $this->_params)) ? $this->_params['CHeyaCD'] : null;
		$searchKbnStock = (array_key_exists('SrhKbnStock', $this->_params)) ? $this->_params['SrhKbnStock'] : null;

		//=====タイトル=====
		$this->setTitle('賃貸情報アパマンショップ［マイページ｜新規登録確認］');

		//=====ヘッダタイトル=====
		$this->setHeaderTitle('賃貸住宅・賃貸マンション・賃貸アパートの検索ならアパマンショップ。全国の賃貸住宅、賃貸不動産情報が満載です。');

		//=====タイトルバー設定=====
		if (Apm_Otheritems::IsNullOrEmpty($formParam) == false)
		{
			$this->setContentsTitle('地域から探す　検索条件を登録', 'class=\'orange\'');
		}
		elseif (Apm_Otheritems::IsNullOrEmpty($heyaCd) == false)
		{
			$this->setContentsTitle('物件詳細　お気に入りに登録', 'class=\'orange\'');
		}
		else
		{
			$this->setContentsTitle('マイページ　新規登録確認', '');
		}

		//=====表示用のテキスト設定（パスワード)=====
		$Passcount=strlen($Pass);
		$strPass="";
		for ($i = 1; $i <= $Passcount; $i++) {
			$strPass.="●";
		}

		//=====表示用のテキスト設定（性別)=====
		if($Sex==1)
		{
			$strSex ="男";
		}
		else if($Sex==2)
		{
			$strSex ="女";
		}
		else
		{
			$strSex ="－";
		}

		//=====表示用のテキスト設定（年齢)=====
		if($Age==1)
		{
			$strAge ="～19";
		}
		else if($Age==2)
		{
			$strAge ="20～29";
		}
		else if($Age==3)
		{
			$strAge ="30～39";
		}
		else if($Age==4)
		{
			$strAge ="40～49";
		}
		else if($Age==5)
		{
			$strAge ="50～59";
		}
		else if($Age==6)
		{
			$strAge ="60～";
		}
		else
		{
			$strAge ="－";
		}

		//=====表示用のテキスト設定（メール送付)=====
		if($Info==1)
		{
			$strInfo ="はい";
		}
		else
		{
			$strInfo ="いいえ";
		}

		//=====表示用のテキスト設定（HTMLメール)=====
		if($HTMLMail==1)
		{
			$strHTMLMail ="はい";
		}
		else
		{
			$strHTMLMail ="いいえ";
		}


		//なんか色々
		$this->_data['strFormParam'] = $formParam;
		$this->_data['strCHeyaCD'] = $heyaCd;
		$this->_data['strSrhKbnStock'] = $searchKbnStock;
		$this->_data['eID'] = $id;
		$this->_data['ePass'] = $Pass;
		$this->_data['strPass'] = $strPass;
		$this->_data['ePass2'] = $Pass2;
		$this->_data['eNickName'] = $NickName;
		$this->_data['eSex'] = $Sex;
		$this->_data['strSex'] = $strSex;
		$this->_data['eAge'] = $Age;
		$this->_data['strAge'] = $strAge;
		$this->_data['eInfo'] = $Info;
		$this->_data['strInfo'] = $strInfo;
		$this->_data['eHTMLMail'] = $HTMLMail;
		$this->_data['strHTMLMail'] = $strHTMLMail;
		$this->_data['MyUcd'] = $MyUcd;


		$this->_breadcrumb->reset();
        $this->_breadcrumb->add(Apm_Config_Search::BREADCRUMB_TOP, $this->_baseurl);
        $this->_breadcrumb->add('マイページ', $this->_baseurl . 'mypage/menu');
        $this->_breadcrumb->add('新規登録', $this->_sslurl . 'mypage/mypagereg');
        $this->setBreadCrumb($this->_breadcrumb->generateBreadcrumb());
        $this->addMetaCss('/css/inquiry/hp/form.css');

	}

	/**
	 * マイページ新規登録完了画面の共通パラメータ設定
	 *
	 */
	private function setCommonParams_fin()
	{
        //ID(e-mail)
        $id = (array_key_exists('eID', $this->_params)) ? $this->_params['eID'] : null;
        $Pass = (array_key_exists('ePass', $this->_params)) ? $this->_params['ePass'] : null;
        $Pass2 = (array_key_exists('ePass2', $this->_params)) ? $this->_params['ePass2'] : null;
        $NickName = (array_key_exists('eNickName', $this->_params)) ? $this->_params['eNickName'] : null;
        $Sex = (array_key_exists('eSex', $this->_params)) ? $this->_params['eSex'] : null;
        $Age = (array_key_exists('eAge', $this->_params)) ? $this->_params['eAge'] : null;
        $Info = (array_key_exists('eInfo', $this->_params)) ? $this->_params['eInfo'] : null;
        $HTMLMail = (array_key_exists('eHTMLMail', $this->_params)) ? $this->_params['eHTMLMail'] : null;
        $MyUcd = (array_key_exists('MyUcd', $this->_params)) ? $this->_params['MyUcd'] : null;

		//TODO:formParam、CHeyaCD、SrhKbnStockはいらないと思う。。。
        //検索条件からのパラメータ引きずり
		$formParam = (array_key_exists('formParam', $this->_params)) ? $this->_params['formParam'] : null;
        //Cookie登録上限部屋CD
		$heyaCd = (array_key_exists('CHeyaCD', $this->_params)) ? $this->_params['CHeyaCD'] : null;
		$searchKbnStock = (array_key_exists('SrhKbnStock', $this->_params)) ? $this->_params['SrhKbnStock'] : null;

		//=====タイトル=====
		$this->setTitle('賃貸情報アパマンショップ［マイページ｜新規登録完了］');

		//=====ヘッダタイトル=====
		$this->setHeaderTitle('賃貸住宅・賃貸マンション・賃貸アパートの検索ならアパマンショップ。全国の賃貸住宅、賃貸不動産情報が満載です。');

		//=====タイトルバー設定=====
		if (Apm_Otheritems::IsNullOrEmpty($formParam) == false)
		{
			$this->setContentsTitle('地域から探す　検索条件を登録', 'class=\'orange\'');
		}
		elseif (Apm_Otheritems::IsNullOrEmpty($heyaCd) == false)
		{
			$this->setContentsTitle('物件詳細　お気に入りに登録', 'class=\'orange\'');
		}
		else
		{
			$this->setContentsTitle('マイページ　新規登録完了', '');
		}

		//なんか色々
		$this->_data['strFormParam'] = $formParam;
		$this->_data['strCHeyaCD'] = $heyaCd;
		$this->_data['strSrhKbnStock'] = $searchKbnStock;
		$this->_data['eID'] = $id;
		$this->_data['ePass'] = $Pass;
		$this->_data['ePass2'] = $Pass2;
		$this->_data['eSex'] = $Sex;
		$this->_data['eAge'] = $Age;
		$this->_data['eInfo'] = $Info;
		$this->_data['eHTMLMail'] = $HTMLMail;
		$this->_data['MyUcd'] = $MyUcd;

		$this->_breadcrumb->reset();
        $this->_breadcrumb->add(Apm_Config_Search::BREADCRUMB_TOP, $this->_baseurl);
        $this->_breadcrumb->add('マイページ', $this->_baseurl . 'mypage/menu');
        $this->_breadcrumb->add('新規登録', $this->_sslurl . 'mypage/mypagereg');
        $this->setBreadCrumb($this->_breadcrumb->generateBreadcrumb());

        //
        $this->addMetaCss('/css/inquiry/hp/form.css');
	}

	/**
	 * セッション取得
	 * @return Zend_Session_Namespace
	 */
	private function getSession()
	{
        return new Zend_Session_Namespace('MypageConf');
	}
}
?>
